package com.gitee.wsl.collections.bag

import com.gitee.wsl.collections.bag.api.Bag


/**
 * Implements `Bag`, using a [HashMap] to provide the
 * data storage. This is the standard implementation of a bag.
 *
 *
 * A `Bag` stores each object in the collection together with a
 * count of occurrences. Extra methods on the interface allow multiple copies
 * of an object to be added or removed at once. It is important to read the
 * interface Javadoc carefully as several methods violate the
 * [Collection] interface specification.
 *
 *
 * @param <E> the type of elements in this bag
 * @since 3.0 (previously in main package v2.0)
</E> */
class HashBag<E> : AbstractMapBag<E>{
    /**
     * Constructs an empty [HashBag].
     */
    constructor() : super(HashMap())

    /**
     * Constructs a bag containing all the members of the given Collection.
     *
     * @param collection a collection to copy into this bag.
     */
    constructor(collection: Collection<E>) : this() {
        addAll(collection)
    }

    /**
     * Constructs a bag containing all the members of the given Iterable.
     *
     * @param iterable an iterable to copy into this bag.
     * @since 4.5.0-M3
     */
    constructor(iterable: Iterable<E>) : super(HashMap(), iterable)

    /**
     * Deserializes the bag in using a custom routine.
     *
     * @param in  the input stream
     * @throws IOException if an error occurs while reading from the stream
     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
     */
//    @Throws(IOException::class, java.lang.ClassNotFoundException::class)
//    private fun readObject(`in`: ObjectInputStream) {
//        `in`.defaultReadObject()
//        super.doReadObject(HashMap(), `in`)
//    }

    /**
     * Serializes this object to an ObjectOutputStream.
     *
     * @param out the target ObjectOutputStream.
     * @throws IOException thrown when an I/O errors occur writing to the target stream.
     */
//    @Throws(IOException::class)
//    private fun writeObject(out: ObjectOutputStream) {
//        out.defaultWriteObject()
//        super.doWriteObject(out)
//    }

}


fun<E> Bag.Companion.newHashBag(): HashBag<E> {
    return HashBag()
}